/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using DataModelException = System.Qizx.Api.DataModelException;
using Node = System.Qizx.Api.Node;
using QName = System.Qizx.Api.QName;
using NamespaceContext = System.Qizx.Util.NamespaceContext;
namespace System.Qizx.Xdm
{
	
	/// <summary> Traverses a Node in pull (streaming) mode.</summary>
	public class NodePullStream:XMLPullStreamBase
	{
		public override QName Name
		{
			get
			{
				try
				{
					return (curNode != null)?curNode.NodeName:null;
				}
				catch (DataModelException e)
				{
					return null;
				}
			}
			
		}
		public override string Text
		{
			get
			{
				try
				{
					if (curNode == null)
						return null;
                    System.Qizx.Api.XdmNodeType nat = curNode.NodeType;
					// avoid returning string value on large nodes:
					return (nat == System.Qizx.Api.XdmNodeType.Document || nat == System.Qizx.Api.XdmNodeType.Element)?null:curNode.StringValue;
				}
				catch (DataModelException e)
				{
					return null;
				}
			}
			
		}
		private Node rootNode;
		private Node curNode;
		
		public NodePullStream(Node rootNode)
		{
			this.rootNode = rootNode;
			curEvent = System.Qizx.Api.XMLPullStream_Fields.START;
		}
		
		public override int moveToNextEvent()
		{
			Node next;
			switch (curEvent)
			{
				
				case System.Qizx.Api.XMLPullStream_Fields.START: 
					if (rootNode == null)
						return setEvent(System.Qizx.Api.XMLPullStream_Fields.END);
					return toNodeStart(rootNode);
				
				case System.Qizx.Api.XMLPullStream_Fields.END: 
					return System.Qizx.Api.XMLPullStream_Fields.END;
				
				case System.Qizx.Api.XMLPullStream_Fields.DOCUMENT_START: 
					next = curNode.FirstChild;
					if (next == null)
						return setEvent(System.Qizx.Api.XMLPullStream_Fields.DOCUMENT_END);
					return toNodeStart(next);
				
				case System.Qizx.Api.XMLPullStream_Fields.DOCUMENT_END: 
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.END);
				
				case System.Qizx.Api.XMLPullStream_Fields.ELEMENT_START: 
					next = curNode.FirstChild;
					if (next == null)
						return setEvent(System.Qizx.Api.XMLPullStream_Fields.ELEMENT_END);
					return toNodeStart(next);
				
				case System.Qizx.Api.XMLPullStream_Fields.ELEMENT_END: 
				case System.Qizx.Api.XMLPullStream_Fields.TEXT: 
				case System.Qizx.Api.XMLPullStream_Fields.PROCESSING_INSTRUCTION: 
				case System.Qizx.Api.XMLPullStream_Fields.COMMENT: 
					next = curNode.NextSibling;
					if (next != null)
						return toNodeStart(next);
					if (rootNode.Equals(curNode))
						return setEvent(System.Qizx.Api.XMLPullStream_Fields.END);
					next = curNode.ParentNode;
					if (next == null)
						return setEvent(System.Qizx.Api.XMLPullStream_Fields.END);
					curNode = next;
					return setEvent(next.NodeType == System.Qizx.Api.XdmNodeType.Element?System.Qizx.Api.XMLPullStream_Fields.ELEMENT_END:System.Qizx.Api.XMLPullStream_Fields.DOCUMENT_END);
				
				default: 
					throw new System.SystemException("wrong state " + curEvent);
				
			}
		}
		
		// Sets the position to the beginning of a Node
		private int toNodeStart(Node node)
		{
			// assert(node != null)
			curNode = node;
			switch (node.NodeType)
			{
				
				case System.Qizx.Api.XdmNodeType.Document: 
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.DOCUMENT_START);
				
				case System.Qizx.Api.XdmNodeType.Element: 
					// attributes and NS set in lazy mode:
					attrCount = nsCount = - 1;
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.ELEMENT_START);
				
				case System.Qizx.Api.XdmNodeType.Text: 
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.TEXT);
				
				case System.Qizx.Api.XdmNodeType.Comment: 
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.COMMENT);
				
				case System.Qizx.Api.XdmNodeType.ProcessingInstruction: 
					piTarget = node.NodeName.Name;
					return setEvent(System.Qizx.Api.XMLPullStream_Fields.PROCESSING_INSTRUCTION);
				
				default: 
					throw new System.SystemException("unimplemented node type " + node.NodeType);
				
			}
		}
		
		protected internal override void  lazyAttr()
		{
			try
			{
				// lazy evaluation:
				Node[] attributes = curNode.Attributes;
				attrCount = 0;
				if (attributes != null)
					for (int a = 0, size = attributes.Length; a < size; a++)
					{
						addAttribute(attributes[a].NodeName, attributes[a].StringValue);
					}
			}
			catch (DataModelException e)
			{
				attrCount = 0;
			}
		}
		
		protected internal override void  lazyNS()
		{
			BasicNode n = (BasicNode) curNode;
			NamespaceContext ctx = new NamespaceContext();
			try
			{
				nsCount = 0;
				n.AddNamespacesTo(ctx);
				for (int i = 0, size = ctx.size(); i < size; i++)
				{
					addNamespace(ctx.getPrefix(i), ctx.getNamespaceURI(i));
				}
			}
			catch (DataModelException e)
			{
				nsCount = 0;
			}
		}
	}
}